package it.tcm.TeacherClassMate.action;

import java.util.List;

import it.tcm.TeacherClassMate.beans.UserBean;
import it.tcm.TeacherClassMate.facade.UserManageBase;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class ActivationAction extends Action {

	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * 
	 */

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) 
	{		
		
		logger.info("ACTIVATE USER");
		 
		// disable any caching by the browser
		response.setHeader("expires","1");
		response.addHeader("Pragma","no-cache");
		response.addHeader("cache-control","no-store");
		response.addHeader("pragma","no-cache");
		
		String code = (String)request.getParameter("code");	
		String userId = (String)request.getParameter("user");	
		logger.info("code : " + code);
		logger.info("user : " + userId);
		try {	
			 UserManageBase userManage = new UserManageBase();
			 UserBean user = userManage.getUserById(new Integer(userId));
			 List<UserBean>  userList = userManage.getUserBeanByCode(code);		 
			 for (UserBean bean: userList) {
				 if (bean.getEmail().equals(user.getEmail())) {
					 user.setActivated(1);
					 userManage.save(user);
					 logger.info("user : " + user.getEmail() + " activated");
					 break;
				 }
			 }		
		} catch (Exception e) {
			logger.error("error during activationAction");
		}
	 
		return mapping.findForward("activation"); 
		 
	}
	
	
}